// pages/hplist/hplist.js
import {hplistAPI} from '../../api/hplist/index'
import {$await} from '../../utils/util'
import runtime from '../../utils/runtime'
const {imgUrlAvatar} = require('../../config.js')
const img_url = 'https://images.weserv.nl/?url='
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
      endTime:'',
      currentData : 0,
      date:'历史榜单',
      date1:'历史榜单',
      hostList:[],//主持人列表
      programList:[],//节目列表
      // imgUrlAvatar:configPic.imgUrlAvatar,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    const year = new Date().getFullYear()
    const month =(new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1);
    const newDate =year+'-'+month
    this.setData({endTime:newDate})
    this.getHostList(newDate)
    this.getProgramList(newDate)
  },
      // 获取主持人列表
      async getHostList(date){
        
      let [err,res] = await $await(hplistAPI.hostList({type:'host',date:date}))
      if(res){
        res = res.map(item =>{
          return {
            ...item,
            avatar:img_url+imgUrlAvatar+item.avatar
          }
        })
        this.setData({hostList:res})
      }
    },
     // 获取节目列表
     async getProgramList(date){

      let [err,res] = await $await(hplistAPI.programList({type:'programme',date:date}))
      if(res){
        res = res.map(item =>{
          return {
            ...item,
            avatar:img_url+imgUrlAvatar+item.avatar
          }
        })
        this.setData({programList:res})
      }
    },
  toRule({currentTarget:{dataset:{type}}}){
    let url = type == 1?'https://h5.cbct.cn/html/hostRole.html':'https://h5.cbct.cn/html/proRole.html'
    wx.navigateTo({
      url:`/pages/webview/webview?webURL=${url}`
    });
  },
  bindDateChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
    console.log(this)
    this.getHostList(e.detail.value)
  },
  bindDateChange1(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date1: e.detail.value
    })
    this.getProgramList(e.detail.value)
  },
  //获取当前滑块的index
  bindchange:function(e){
    const that  = this;
    that.setData({
      currentData: e.detail.current
    })
  },
  //点击切换，滑块index赋值
  checkCurrent:function(e){
    const that = this;
 
    if (that.data.currentData === e.target.dataset.current){
        return false;
    }else{
 
      that.setData({
        currentData: e.target.dataset.current
      })
    }
  }
})